package cn.orangelite.ai.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.orangelite.ai.domain.AiInitConfigData;
import cn.orangelite.ai.domain.AiReplyConfig;
import cn.orangelite.ai.domain.AiWechatLog;
import cn.orangelite.ai.domain.AiWechatUser;
import cn.orangelite.ai.domain.dto.WechatUserInitDataDTO;
import cn.orangelite.ai.service.IAiInitConfigDataService;
import cn.orangelite.ai.service.IAiWechatLogService;
import cn.orangelite.ai.service.IAiWechatUserService;
import cn.orangelite.common.core.utils.StringUtils;
import cn.orangelite.common.core.web.controller.BaseController;
import cn.orangelite.common.core.web.domain.AjaxResult;
import cn.orangelite.common.core.web.page.TableDataInfo;
import cn.orangelite.common.log.annotation.Log;
import cn.orangelite.common.log.enums.BusinessType;
import cn.orangelite.common.security.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/wechatUser")
public class AiWechatUserController extends BaseController {
    @Value("${langchain.domain}")
    private String langchainDomain;

    @Autowired
    private IAiWechatUserService aiWechatUserService;

    @Autowired
    private IAiInitConfigDataService aiInitConfigDataService;

    @Autowired
    private IAiWechatLogService aiWechatLogService;

    @RequiresPermissions("ai:wechatUser:list")
    @GetMapping("/list")
    public TableDataInfo list(AiWechatUser wechatUser){
        startPage();
        List<AiWechatUser> list = aiWechatUserService.list(Wrappers.<AiWechatUser>lambdaQuery().like(StringUtils.isNotEmpty(wechatUser.getNickname()), AiWechatUser::getNickname, wechatUser.getNickname()));
        return getDataTable(list);
    }

    @RequiresPermissions("ai:wechatUser:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(aiWechatUserService.getById(id));
    }

    @RequiresPermissions("ai:wechatUser:add")
    @Log(title = "knowledge", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AiWechatUser wechatUser) throws Exception {
        return toAjax(aiWechatUserService.save(wechatUser));
    }

    @RequiresPermissions("ai:wechatUser:edit")
    @Log(title = "knowledge", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AiWechatUser wechatUser) throws Exception {
        return toAjax(aiWechatUserService.updateById(wechatUser));
    }

    @RequiresPermissions("ai:wechatUser:remove")
    @Log(title = "knowledge", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) throws Exception {
        List<AiWechatUser> aiWechatUsers = aiWechatUserService.listByIds(Arrays.asList(ids));

        for(AiWechatUser aiWechatUser : aiWechatUsers){
            //删除数据库记录
            aiWechatLogService.remove(Wrappers.<AiWechatLog>lambdaQuery().eq(AiWechatLog::getUsername,aiWechatUser.getUsername()));
            //请求langchain删除redis记录
            String aiResult = HttpUtil.get(langchainDomain+"/gpt/clear_chat_log/"+aiWechatUser.getUsername());
            //删除微信用户
            aiWechatUserService.removeById(aiWechatUser.getId());
        }
        //删除微信用户列表
        return toAjax(aiWechatUsers.size());
    }

    @PostMapping("/getInitData")
    public AjaxResult getInitData(@RequestBody WechatUserInitDataDTO wechatUserInitDataDTO){
        AiWechatUser wechatUser = aiWechatUserService.getOne(Wrappers.<AiWechatUser>lambdaQuery().eq(AiWechatUser::getUsername, wechatUserInitDataDTO.getUsername()));
        if(wechatUser==null){
            aiWechatUserService.save(wechatUserInitDataDTO);
        }
        HashMap<String, Object> result = new HashMap<>();
        AiInitConfigData aiInitConfigData = aiInitConfigDataService.list().get(0);
        result.put("config",aiInitConfigData);
        return success(result);
    }
}
